# -*- coding: utf-8 -*-
"""
@Time    : 2025/2/27 10:20 
@Author  : ZhangShenao 
@File    : 2.WordEmbedding词嵌入.py 
@Desc    : WordEmbedding词嵌入

词嵌入是文本的一种表示方式
它将文本映射到高维的向量空间中,同时可以捕获语义关系
"""

from gensim.models import Word2Vec
from nltk.tokenize import word_tokenize

# 定义训练语料
sentences = [
    "The cat sat on the mat.",
    "Dogs and cats are enemies.",
    "The dog chased the cat."
]

# 使用NLTK进行分词
tokenized_sentences = [word_tokenize(sentence.lower()) for sentence in sentences]
print(f"分词结果: \n{tokenized_sentences}\n")

# 使用gensim库,训练一个Word2Vec模型
# 向量维度设置为50维
model = Word2Vec(sentences=tokenized_sentences, vector_size=50, window=5, min_count=1, workers=4)
# 获取单词“cat”的向量
cat_vector = model.wv['cat']
print(f"cat的向量表示: \n{cat_vector}", )
print(f"向量维度: {len(cat_vector)}", )

# 利用语义相似度检索,找到与“cat”最相似的单词
similar_words = model.wv.most_similar('cat', topn=5)
print(f"和cat相似的单词是: {similar_words}")
